package com.example.consurmer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpEntity;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.http.HttpHeaders;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
@RestController
@EnableEurekaClient
@SpringBootApplication
public class ConsurmerApplication {
    @Autowired
    RestTemplate restTemplate;
    public static void main(String[] args) {
        SpringApplication.run(ConsurmerApplication.class, args);
    }

    /**
     * 实例化RestTemplate
     * @return
     */
    @LoadBalanced
    @Bean
    public RestTemplate rest() {
        return new RestTemplate();
    }

    /**
     * Rest服务端使用RestTemplate发起http请求,然后得到数据返回给前端----gotoUser是为了区分getUser怕小伙伴晕头
     * @param id
     * @return
     */
    @GetMapping(value = "/gotoUser")
    @ResponseBody
    public Map<String,Object> getUser(@RequestParam Integer id){
        Map<String,Object> data = new HashMap<>();
        /**
         * 小伙伴发现没有，地址居然是http://service-provider
         * 居然不是http://127.0.0.1:8082/
         * 因为他向注册中心注册了服务，服务名称service-provider,我们访问service-provider即可
         */
        data = restTemplate.getForObject("http://service-provider/getUser?id="+id,Map.class);
        return data;
    }

    @RequestMapping("/book3")
    @ResponseBody
    public Map<String, Object> book3(HttpServletRequest res, HttpServletResponse rs) {

        HttpHeaders headers = new HttpHeaders();

        headers.setContentType(MediaType.MULTIPART_FORM_DATA);

        headers.setContentType(MediaType.APPLICATION_JSON);//设置参数类型和编码



        Map<String,Object> map1 = new HashMap<>();//建立简单的String，String的map

        map1.put("email", "first.last@example.com");

        map1.put("email1", "first.last@example.com1");

        map1.put("email2", "first.last@example.com2");

        HttpEntity<Map<String,Object>> request1 = new HttpEntity<>(map1, headers);//包装到HttpEntity

        //postForEntity  -》 直接传递map参数


        //postForObject  -》 直接传递map参数

        try {
            Map<String, Object> respo3 = restTemplate.postForObject(
                    "http://service-provider/getbook", request1,
                    Map.class);
            return respo3;
        }
        catch (Exception e)
        {
            System.out.println(e.toString());
        }
        //postForObject  -》 传递httpentity参数

    return  null;

    }
}
